package gmew.client;

import gmew.shared.ItemDTO;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.util.ValueCallback;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;

public class ItemDbPanel {
	private static ItemDbPanel sInstance = null;

	private Tab mTab = new Tab("Item Database");
	private ListGrid mItemsGrid = new ListGrid();
	
	static {
		Gmew.getTabSet().addCloseClickHandler(new CloseClickHandler() {
			@Override
			public void onCloseClick(TabCloseClickEvent event) {
				if (event.getTab() == sInstance.mTab) {
					sInstance = null;
				}
			}
		});
	}
	
	private AsyncCallback<List<ItemDTO>> mItemsRefreshCallback = new AsyncCallback<List<ItemDTO>>() {
		@Override
		public void onFailure(Throwable caught) {
			SC.warn(caught.getMessage());
		}

		@Override
		public void onSuccess(List<ItemDTO> result) {
			ListGridRecord[] records = new ListGridRecord[result.size()];

			System.out.println("records found: " + result.size());
			
			int n = 0;
			for (ItemDTO i : result) {
				ListGridRecord record = new ListGridRecord();
				record.setAttribute("itemId", i.getId());
				record.setAttribute("itemLink", "http://www.wowhead.com/item=" + i.getId());
				record.setAttribute("itemName", i.getName());
				record.setLinkText(i.getName());
				System.out.println("record #" + n + " id:" + i.getId() + " " + i.getName());
				records[n++] = record;
			}
			mItemsGrid.setData(records);
		}
	};

	private ItemDbPanel() {
		VLayout tabPane = new VLayout();

		IButton addItemButton = new IButton("Add Item");
		addItemButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				SC.askforValue("Add Item", "Enter Item Id to Add:", new ValueCallback() {
					@Override
					public void execute(String value) {
						Gmew.service.addItemFromWowhead(Integer.parseInt(value), new AsyncCallback<Void>() {
							@Override
							public void onFailure(Throwable caught) {
								SC.warn(caught.getMessage());
							}

							@Override
							public void onSuccess(Void result) {
								refreshGrid();
							}
						});
					}
				});
			}
		});

		IButton refreshButton = new IButton("Refresh Data");
		refreshButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				refreshGrid();
			}
		});
		
		HLayout hLayout = new HLayout();
		hLayout.setMembersMargin(10);
		hLayout.addMember(addItemButton);
		hLayout.addMember(refreshButton);
		hLayout.setAutoWidth();
		tabPane.addMember(hLayout);

		mItemsGrid.setWidth100();
		mItemsGrid.setHeight100();
		mItemsGrid.setShowAllRecords(true);

		ListGridField itemIdField = new ListGridField("itemId", "Id");
		ListGridField itemLinkField = new ListGridField("itemLink", "Link");
		ListGridField itemNameField = new ListGridField("itemName", "Name");
		itemLinkField.setAlign(Alignment.CENTER);
		itemLinkField.setType(ListGridFieldType.LINK);
		// itemLinkField.setLinkText("Link");
		mItemsGrid.setFields(itemIdField, itemNameField, itemLinkField);

		refreshGrid();
		tabPane.addMember(mItemsGrid);

		mTab.setPane(tabPane);
		mTab.setCanClose(true);
	}

	public Tab getTab() {
		return mTab;
	}
	
	private void refreshGrid() {
		Gmew.service.getAllItems(mItemsRefreshCallback);
	}

	public static void activate() {
		if (sInstance == null) {
			sInstance = new ItemDbPanel();
			Gmew.getTabSet().addTab(sInstance.getTab());
		}
		Gmew.getTabSet().selectTab(sInstance.getTab());
	}
}
